package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.provider.manager.mapper.MusicTblMapper;
import com.amigo.online.provider.manager.model.entity.MusicInfo;
import com.amigo.online.provider.manager.model.entity.MusicTbl;
import com.amigo.online.provider.manager.service.MusicTblService;

@Service
public class MusicTblServiceImpl extends BaseService<MusicTbl> implements MusicTblService {


    @Resource
    private MusicTblMapper musicTblMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    private static String MUSIC_KEY = "o:v:m:m";
    private static String MUSIC_REDIS_ID_KEY = "o:v:m:m:id";

    @Override
    public boolean saveMusic(MusicTbl musicTbl) {
        int i = musicTblMapper.saveMusicTbl(musicTbl);
        if (i <= 0) {
            return false;
        } else {
            MusicInfo musicInfo = new MusicInfo();
            musicInfo.setId(musicTbl.getId());
            musicInfo.setName(musicTbl.getName());
            musicInfo.setMusic(musicTbl.getMusic());
            musicInfo.setCreatedTime(musicTbl.getCreatedTime());
            if (musicTbl.getUserId() != null) {
                musicInfo.setUserId((long) (musicTbl.getUserId()));
            }
            if (musicTbl.getCategory() != null) {
                musicInfo.setCategory(musicTbl.getCategory());
            }
            if (musicTbl.getCover() != null) {
                musicInfo.setCover(musicTbl.getCover());
            }
            if (musicTbl.getRecommended() != null) {
                musicInfo.setRecommended(musicTbl.getRecommended());
            }
            if (musicTbl.getUsed() != null) {
                musicInfo.setUsed(musicTbl.getUsed());
            }
            if (musicTbl.getType() != null) {
                musicInfo.setType(musicTbl.getType());
            }
            if (musicTbl.getAuthor() != null) {
                musicInfo.setAuthor(musicTbl.getAuthor());
            }
            musicInfo.setCollected(0);


            redisTemplate.opsForHash().put(MUSIC_KEY, musicTbl.getId(), musicInfo);
            redisTemplate.opsForList().leftPush(MUSIC_REDIS_ID_KEY, musicTbl.getId());
            return true;
        }
    }

    @Override
    public List<MusicTbl> findMusicList(MusicTbl musicTbl) {
        return musicTblMapper.findMusicList(musicTbl);
    }

    @Override
    public boolean deleteMusic(MusicTbl musicTbl) {


        musicTbl.setDeletedTime(System.currentTimeMillis());
        int i = musicTblMapper.deleteMusic(musicTbl);
        if (i <= 0) {
            return false;
        } else {
            redisTemplate.opsForHash().delete(MUSIC_KEY, musicTbl.getId());
            redisTemplate.opsForList().remove(MUSIC_REDIS_ID_KEY, -1, musicTbl.getId());
            return true;
        }
    }

    @Override
    public boolean updateMusic(MusicTbl musicTbl) {
        int i = musicTblMapper.updateMusic(musicTbl);
        if (i <= 0) {
            return false;
        } else {
            MusicInfo musicInfo = new MusicInfo();
            musicInfo.setId(musicTbl.getId());
            musicInfo.setName(musicTbl.getName());
            musicInfo.setMusic(musicTbl.getMusic());
            musicInfo.setCreatedTime(musicTbl.getCreatedTime());
            if (musicTbl.getUserId() != null) {
                musicInfo.setUserId((long) (musicTbl.getUserId()));
            }
            if (musicTbl.getCategory() != null) {
                musicInfo.setCategory(musicTbl.getCategory());
            }
            if (musicTbl.getCover() != null) {
                musicInfo.setCover(musicTbl.getCover());
            }
            if (musicTbl.getRecommended() != null) {
                musicInfo.setRecommended(musicTbl.getRecommended());
            }
            if (musicTbl.getUsed() != null) {
                musicInfo.setUsed(musicTbl.getUsed());
            }
            if (musicTbl.getType() != null) {
                musicInfo.setType(musicTbl.getType());
            }
            if (musicTbl.getAuthor() != null) {
                musicInfo.setAuthor(musicTbl.getAuthor());
            }
            musicInfo.setCollected(0);
            redisTemplate.opsForHash().put(MUSIC_KEY, musicTbl.getId(), musicInfo);
            return true;
        }
    }

}
